package ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import protocal.DataPackage;
import protocal.DataPackage.MessageType;
import util.MessageSender;
import client.ChatClient;

import com.fasterxml.jackson.databind.ObjectMapper;

import data.User;

public class LoginFrame extends JFrame implements ActionListener{
	
	private static final long serialVersionUID = 1L;
	private final static Logger logger = LogManager.getLogger(LoginFrame.class);
	
	public enum Status{
		LOGIN,REGISTER,ISLOGIN
	}
	
	private JTextField nameTf;
	private JPasswordField psdPf;
	private JButton loginBtn;
	private JButton registerlBtn;
	
	private ChatFrame chatFrame;
	private RegisterFrame registerFrame;
	
	public LoginFrame(){
		this.setSize(400,260);
		this.setResizable(false);
		init();
		addEvent();
		this.setVisible(true);
	}
	
	public void init(){
		this.setLayout(null);
		this.setLocationRelativeTo(null);
		nameTf = new JTextField();
		psdPf = new JPasswordField();
		JLabel nameLable = new JLabel("username:",nameTf.LEFT);
		JLabel psdfLable = new JLabel("password:",psdPf.LEFT);
		loginBtn = new JButton("login");
		registerlBtn = new JButton("register");
		nameLable.setBounds(10, 20, 80, 40);
		nameTf.setBounds(80,20,250,30);
		psdfLable.setBounds(10, 80, 80, 40);
		psdPf.setBounds(80, 80, 250, 30);
		loginBtn.setBounds(60, 140, 80, 40);
		registerlBtn.setBounds(200,140,80,40);
		this.add(nameLable);
		this.add(psdfLable);
		this.add(nameTf);
		this.add(psdPf);
		this.add(loginBtn);
		this.add(registerlBtn);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public void setChatFrame(ChatFrame chatFrame) {
		this.chatFrame = chatFrame;
	}

	public void setRegisterFrame(RegisterFrame registerFrame) {
		this.registerFrame = registerFrame;
	}

	private void addEvent(){
		loginBtn.addActionListener(this);
		registerlBtn.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
				registerFrame.setVisible(true);
			}
		});
	}
	
	public void actionPerformed(ActionEvent e) {
		DataPackage dataPackage = new DataPackage();
		dataPackage.setFromeName(nameTf.getText());
		dataPackage.setToName("\\");
		dataPackage.setMessageType(MessageType.LOGIN);
		dataPackage.setMessageData(psdPf.getText());
		dataPackage.setTime(new Date().getTime());
		new User(nameTf.getText());
		MessageSender.send(dataPackage);
		ObjectMapper mapper = new ObjectMapper();
		try {
			BufferedReader reader = ChatClient.getInstance().getReader();
			String data = reader.readLine();
			DataPackage response =  mapper.readValue(data, DataPackage.class);
			logger.info(response.toString());
			if (response.getMessageData().equals("success")) {
				setVisible(false);
				chatFrame.setVisible(true);
				chatFrame.listene();
				chatFrame.loginText();
			}else{
				JOptionPane.showMessageDialog(null, response.getMessageData(), "alert", JOptionPane.ERROR_MESSAGE);
			}
		} catch (IOException e1) {
			logger.error(e1);
		}
		
	}
	

	public static void main(String[] args) {
		new LoginFrame();
	}

}
